<?php

namespace App\Http\Controllers;

use App\Models\Video;
use Illuminate\Http\Request;

class SearchController extends Controller
{
    public function index(Request $request)
    {
        $search_list = $request->session()->get('user.search');

        $news = Video::query()->latest('updated_at')->limit(10)->get();
        $hots = Video::query()->latest('visits')->limit(10)->get();

        return view('search', compact('news', 'hots', 'search_list'));
    }

    public function history(Request $request)
    {
        $request->validate([
            'search' => 'required|string|max:100',
        ]);

        $search = $request->string('search')->value();

        $request->session()->push('user.search', $search);

        return to_route('videos.index', compact('search'));
    }

    public function clear(Request $request)
    {
        $request->session()->remove('user.search');

        return to_route('search.index');
    }
}
